﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Routing;
using TiTi.tea.Api.Attributes;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Comm;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/order")]
    public class OrderController : ApiController
    {
        private OrderInfoBLL bll = new OrderInfoBLL();

        /// <summary>
        /// 出餐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("outOrder")]
        [HttpPost]
        public Result OutOrder(long id)
        {
            if (bll.UpdateStatus(id, 3))
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("修改失败");
            }
        }

        /// <summary>
        /// 获取全部订单
        /// </summary>
        /// <returns></returns>
        [Route("getAll")]
        [HttpGet]
        public Result GetAll()
        {
            var data = bll.Get();
            return Result.Success(data);
        }

        /// <summary>
        /// 获取用户已支付的订单
        /// </summary>
        /// <returns></returns>
        [Route("getBoughtOrder")]
        [HttpGet]
        public Result GetBoughtOrder()
        {
            var data = bll.Get(2);

            return Result.Success(data);
        }

        /// <summary>
        /// 获取用户今日订单
        /// </summary>
        /// <returns></returns>
        [Route("GetOrderInfo")]
        [HttpGet]
        [MyAuthorize]
        public Result GetOrderInfo()
        {
            IHttpRouteData routeData = Request.GetRouteData();
            IDictionary<string, object> dict = routeData.Values;
            var auth = dict["auth"] as AuthInfo;
            var userId = auth.UserId;

            var data = bll.GetOrderinfos(userId);
            return Result.Success(data);
        }

        /// <summary>
        /// 获取用户历史订单
        /// </summary>
        /// <returns></returns>
        [Route("GetCustomerOrders")]
        [HttpGet]
        [MyAuthorize]
        public Result GetCustomerOrders()
        {
            IHttpRouteData routeData = Request.GetRouteData();
            IDictionary<string, object> dict = routeData.Values;
            var auth = dict["auth"] as AuthInfo;
            var userId = auth.UserId;

            var data = bll.GetCustomerOrders(userId);
            return Result.Success(data);
        }

        /// <summary>
        /// 修改订单状态为已支付
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("buy")]
        [HttpPost]
        public Result OrderBuy(long id)
        {
            if (bll.OrderBuy(id)) return Result.Success();
            else return Result.Error("修改失败");
        }

        [Route("GetOrderInfoDetails")]
        [HttpGet]
       public Result GetOrderInfoDetails(long id)
        {
            var data = bll.GetOrderInfoDetails(id);
            return Result.Success(data);
        }

        public Result Get(long id)
        {
            var data = bll.Get(id);
            if (data == null)
            {
                return Result.Error("数据不存在");
            }
            return Result.Success(data);
        }
    }
}